﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExamAnalysis.Model;
using System.Web.Security;
using ExamAnalysis.Core;
using ExamAnalysis.Core.Statistics;
using Ext.Net;
using System.IO;

namespace ExamAnalysis.Reports
{
    public partial class PicReport : System.Web.UI.Page
    {
        static int ExamID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            //string teacher = "1";
            string teacher = Membership.GetUser().UserName;
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var score = (from s in db.StudentInExam
                             join st in db.StudentInfo on s.StudentID equals st.StudentID
                             join t in db.TeacherInClass on st.ClassID equals t.ClassID
                             where t.TeacherInfo.AccountID == teacher
                             select new { ExamID = s.ExamID, ExamName = s.ExamInfo.ExamName, ClassID = st.ClassID, ClassName = st.ClassInfo.ClassName }).Distinct();
                Store1.DataSource = score;
                Store1.DataBind();
                if (ExamID != 0)
                {
                    Table.Visible = true;
                    var score2 = from s in db.StudentInExam orderby s.Score
                                 join st in db.StudentInfo on s.StudentID equals st.StudentID
                                 join t in db.TeacherInClass on st.ClassID equals t.ClassID
                                 where t.TeacherInfo.AccountID == teacher && s.ExamID == ExamID
                                 select new { Student = s.StudentInfo.StudentName, Score = s.Score, StudentID = s.StudentID };
                    foreach (var sco in score2)
                    {
                        IntoLable(Convert.ToInt32(sco.Score));
                    }

                    var score3 = from ss in score2
                                 select ss.Score;
                    List<object> stat = new List<object> 
                    {
                        new {Max = score3.Max()-1-score3.Average(),Min=score3.Min()-1,Mean=score3.Average()-1-score3.Min(),MinScore=1,MeanScore=1,MaxScore=1},
                     };
                    Store2.DataSource = stat;
                    Store2.DataBind();
                }
                else
                {
                    Table.Visible = false;
                }

            }
        }


        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (ComboBox1.SelectedItem != null)
            {
                ExamID = Convert.ToInt32(ComboBox1.SelectedItem.Value);
                Response.Redirect("PicReport.aspx");
            }
            else
            {
                X.Msg.Alert("提示信息", String.Format("请选择一个考试")).Show();
            }     
                           
        }

        protected void IntoLable(int i)
        {
            switch (i/10)
            {
                case 0: Label0.Text += " " + i % 10 + " ";
                    break;
                case 1: Label1.Text += " " + i % 10 + " ";
                    break;
                case 2: Label2.Text += " " + i % 10 + " ";
                    break;
                case 3: Label3.Text += " " + i % 10 + " ";
                    break;
                case 4: Label4.Text += " " + i % 10 + " ";
                    break;
                case 5: Label5.Text += " " + i % 10 + " ";
                    break;
                case 6: Label6.Text += " " + i % 10 + " ";
                    break;
                case 7: Label7.Text += " " + i % 10 + " ";
                    break;
                case 8: Label8.Text += " " + i % 10 + " ";
                    break;
                case 9: Label9.Text += " " + i % 10 + " ";
                    break;
                default: Label10.Text += " " + i % 10 + " ";
                    break;
            }
        }
    }
}